package com.jch.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jch.reggie.common.CustomExcepttion;
import com.jch.reggie.entity.Category;
import com.jch.reggie.entity.Dish;
import com.jch.reggie.entity.Setmeal;
import com.jch.reggie.mapper.CategoryMapper;
import com.jch.reggie.service.CategoryService;
import com.jch.reggie.service.DishService;
import com.jch.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceimpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    /**
     * 根据id删除分类，查询前需要进行判断
     * @param id
     */
    @Override
    public void remove(Long id) {
        //条件构造器
        LambdaQueryWrapper<Dish> DishLamdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Setmeal> SetmealLamdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据id分类查询
        DishLamdaQueryWrapper.eq(Dish::getCategoryId,id);
        SetmealLamdaQueryWrapper.eq(Setmeal::getCategoryId,id);

        int count1 = dishService.count(DishLamdaQueryWrapper);
        int count2 = setmealService.count(SetmealLamdaQueryWrapper);

        //查询当前分类是否关联了菜品，如果已关联，抛出一个业务异常
        if(count1>0){
            //已经关联菜品
            throw new CustomExcepttion("当前分类下关联了菜品，不能删除");
        }
        //查询当前菜品是否关联了套餐，如果已关联，抛出一个业务异常
        if(count2>0){
            //已经关联套餐
            throw new CustomExcepttion("当前分类下关联了套餐，不能删除");

        }
        //正常删除
         super.removeById(id);
    }
}
